//获取应用实例
const app = getApp();

Page({
    data: {
        season_list: [],
        order_list: [],
        now_page: 0,
        limit: 30,
        keywords: '',
        list_total: 0,
        season_id: -1,
        year: [],
        yearArray: [],
        is_show_shade: 0,
        cate_show_data: [],
        cate_all_data: [],
        sel_index_arr: [0, 0, 0],
        choose_index_arr: [],
        choose_id_arr: [],
        choose_cate_name: [],
        data_type_list: [
            {id: 1, name: '全部', value: 0},
            {id: 2, name: '正品', value: 1},
            {id: 3, name: '白批', value: 2},
        ],
        data_type_name: '',
        data_type_value: 0,
        // 表格相关
        table_scroll_left: 0,
        table_head: [
            {id: 1, name: '货号', field: 'GoodsNo', is_checked: true, width: 155},
            {id: 2, name: '款名', field: 'GoodsName', is_checked: true, width: 160},
            {id: 3, name: '供应商', field: 'SupplyNames', is_checked: true, width: 150},
            {id: 4, name: '下单数', field: 'caigou_sum', is_checked: true, width: 120},
            {id: 5, name: '入库数', field: 'shouhuo_sum', is_checked: true, width: 120},
            {id: 6, name: '未收数', field: 'weishou_sum', is_checked: true, width: 120},
            {id: 7, name: '颜色', field: 'colorname', is_checked: true, width: 120},
        ],
    },
    onLoad: function () {
        // 生成年份
        this.generateYearArray();
        // 设置标题
        wx.setNavigationBarTitle({
            title: '大货汇总'
        });
        // 获取季节列表
        this.get_season_list(1);
    },

    // 显示时
    onShow: function () {
        var __this = this;
        // 验证是否登录
        if (!app.check_is_login(false, true)) {
            // 清空数据
            __this.setData({
                season_list: [],
                order_list: [],
                now_page: 0,
                limit: 50,
                list_total: 0,
                keywords: '',
                season_id: -1,
                choose_index_arr: [],
                choose_id_arr: [],
                choose_cate_name: [],
            });
            // 重新获取数据
            wx.showModal({
                title: '提示',
                content: '请登录',
                complete(res) {
                    // 获取季节列表
                    __this.get_season_list();
                }
            });
        }
    },

    // 页面触底时执行
    onReachBottom: function () {
        // if (this.data.season_id > -1) {
        //     this.get_order_list();
        // }
    },

    // 监听下拉[刷新]
    onPullDownRefresh: function () {
        this.setData({
            season_list: [],
            order_list: [],
            now_page: 0,
            limit: 100,
            list_total: 0,
            keywords: '',
            season_id: -1,
            choose_index_arr: [],
            choose_id_arr: [],
            choose_cate_name: [],
        });
        // 获取季节
        this.get_season_list();
        // 停止下拉
        setTimeout(() => {
            wx.stopPullDownRefresh();
        }, 1000)
    },

    // 表格滑动结束
    tr_boxs_scroll_end: function (e) {
        var __this = this;
        setTimeout(function () {
            __this.setData({
                table_scroll_left: e.detail.scrollLeft
            });
        }, 180);
    },

    // 获取季节列表
    get_season_list: function (is_get_cate = 0) {
        let req_data = {}
        if (this.data.year.length > 0) {
            req_data.year = this.data.year;
        }
        // 分类
        var category1 = [];
        var category2 = [];
        var category3 = [];
        if (this.data.choose_id_arr.length > 0) {
            this.data.choose_id_arr.forEach(function (item, index) {
                category1.push(item[0]);
                category2.push(item[1]);
                category3.push(item[2]);
            });
        }
        req_data.category1 = category1;
        req_data.category2 = category2;
        req_data.category3 = category3;
        // 数据类型
        req_data.data_type = this.data.data_type_value;
        console.log(req_data)
        // 发起请求
        var result = app.request_api('admin/template/goodsSummary', req_data);
        result.then(res => {
            console.log(res)
            if (res.length > 0) {
                res.forEach(function (item, index) {
                    res[index].is_show = 0;
                });
            }
            this.setData({
                season_list: res
            });
            // 获取分类
            if (is_get_cate == 1) {
                this.get_cate_list();
            }
        });
    },

    // 获取分类
    get_cate_list: function () {
        // 发起请求
        var result = app.request_api('admin/GoodsCategory/index', {
            is_mini: 1
        }, false);
        result.then(res => {
            // console.log(res);
            if (res.list.length > 0) {
                res.list.forEach(function (item, index) {
                    // 往子项里追加全部
                    res.list[index].children.unshift({
                        id: 0,
                        category_name: '全部',
                        children: [],
                    });
                    res.list[index].children.forEach(function (item2, index2) {
                        res.list[index].children[index2].children.unshift({
                            id: 0,
                            category_name: '全部'
                        });
                    });
                });
                this.setData({
                    cate_show_data: [res.list, res.list[0].children, res.list[0].children[0].children],
                    cate_all_data: res.list
                });
                // console.log(res.list)
            }
        });
    },

    // 生成年份
    generateYearArray: function () {
        var currentYear = new Date().getFullYear();
        var pastYears = 7;
        var futureYears = 3;
        var yearArray = [];

        // 生成过去7年的年份
        for (var i = currentYear - pastYears; i <= currentYear; i++) {
            yearArray.push(i);
        }

        // 生成未来3年的年份
        for (var j = currentYear + 1; j <= currentYear + futureYears; j++) {
            yearArray.push(j);
        }
        var currentIndex = pastYears;
        // 更新数据
        this.setData({
            yearArray: yearArray,
            // year:currentYear,
            currentIndex: currentIndex
        });
    },

    // 年份改变时
    yearChange: function (event) {
        var selectedYear = this.data.yearArray[event.detail.value];
        // 验证是否在数组中
        if (this.data.year.findIndex(obj => obj === selectedYear) > -1) {
            wx.showToast({
                title: '已选择过此年份',
                icon: 'none',
                duration: 1500,
            });
            return false;
        }
        this.data.year.push(selectedYear)
        this.setData({
            year: this.data.year,
        });
        console.log(this.data.year)
        // 其他处理逻辑
        this.get_season_list()
    },

    // 显示或隐藏订单列表
    toggle_show_list: function (events) {
        var __this = this;
        var index = events.currentTarget.dataset.index;
        var is_show = __this.data.season_list[index].is_show == 1 ? 0 : 1;
        var season_id = -1;
        // 显示或隐藏时
        if (is_show == 1) {
            season_id = __this.data.season_list[index].value;
        }
        // 关闭全部
        __this.data.season_list.forEach(function (item, index2) {
            __this.data.season_list[index2].is_show = 0;
        });
        // 展示自己
        __this.data.season_list[index].is_show = is_show;
        // 重置
        __this.setData({
            season_list: __this.data.season_list,
            season_id: season_id,
            order_list: [],
            now_page: 0,
            list_total: 0,
            keywords: '',
        });
        // 请求获取订单信息
        if (is_show == 1) {
            this.get_order_list();
        }
    },

    // 获取订单信息
    get_order_list: function (is_search = 0) {
        var __this = this;
        // 验证是否超出最大页码
        if ((this.data.order_list.length >= this.data.list_total) && this.data.now_page > 0) {
            return false;
        }
        // 请求参数
        var req_data = {
            page: this.data.now_page + 1,
            GoodsCode: __this.data.keywords,
            limit: __this.data.limit,
            season: __this.data.season_id,
            data_type: __this.data.data_type_value,
        };
        // 年份
        if (this.data.year.length > 0) {
            req_data.year = this.data.year;
        }
        // 分类
        var category1 = [];
        var category2 = [];
        var category3 = [];
        if (this.data.choose_id_arr.length > 0) {
            this.data.choose_id_arr.forEach(function (item, index) {
                category1.push(item[0]);
                category2.push(item[1]);
                category3.push(item[2]);
            });
        }
        req_data.category1 = category1;
        req_data.category2 = category2;
        req_data.category3 = category3;
        console.log(req_data)
        // 发起请求
        var result = app.request_api('admin/Template/goodsList', req_data);
        result.then(res => {
            console.log(res);
            if (is_search == 1) {
                this.setData({
                    order_list: [],
                    list_total: 0,
                    now_page: 0,
                })
            }
            this.setData({
                order_list: this.data.order_list.concat(res.list),
                list_total: res.total,
                now_page: this.data.now_page + 1
            });
        });
    },

    // 搜索
    GoSearch: function (events) {
        var keywords = events.detail.value.trim();
        this.setData({
            keywords: keywords,
            order_list: [],
            list_total: 0,
            now_page: 0,
        });
        this.get_order_list(1);
    },

    // 预览图片
    PreviewImg: function (events) {
        var pic_arr = events.currentTarget.dataset.pic;
        if (pic_arr.length == 0) {
            return false;
        }
        // 调用
        wx.previewImage({
            current: pic_arr[0],
            urls: pic_arr
        })
    },

    // 分类值变化时
    CateValueChange: function (e) {
        // console.log(e);
        var cate_show_data = this.data.cate_show_data;
        var choose_index_arr = e.detail.value;
        var choose_cate_name = cate_show_data[0][choose_index_arr[0]].category_name + ' - ' + cate_show_data[1][choose_index_arr[1]].category_name  + ' - ' + cate_show_data[2][choose_index_arr[2]].category_name;
        // 更新值
        this.data.choose_id_arr.push([cate_show_data[0][choose_index_arr[0]].id, cate_show_data[1][choose_index_arr[1]].id, cate_show_data[2][choose_index_arr[2]].id]);
        this.data.choose_cate_name.push(choose_cate_name);
        this.data.choose_index_arr.push(choose_index_arr);
        this.setData({
            // choose_id_arr: [cate_show_data[0][choose_index_arr[0]].id, cate_show_data[1][choose_index_arr[1]].id, cate_show_data[2][choose_index_arr[2]].id],
            // choose_index_arr: choose_index_arr,
            // choose_cate_name: choose_cate_name,
            choose_id_arr: this.data.choose_id_arr,
            choose_index_arr: this.data.choose_index_arr,
            choose_cate_name: this.data.choose_cate_name,
            season_list: [],
            order_list: [],
            now_page: 0,
            keywords: '',
            list_total: 0,
        });
        // console.log(this.data.choose_cate_name)
        // console.log(this.data.choose_index_arr)
        // console.log(this.data.choose_id_arr)
        // 重新请求数据
        this.get_season_list();
    },

    // 分类列变化时
    CateColumnChange: function (e) {
        // console.log(e);
        var cate_all_data = this.data.cate_all_data;
        var cate_show_data = this.data.cate_show_data;
        var sel_index_arr = this.data.sel_index_arr;
        var index = e.detail.value;
        if (e.detail.column == 0) {
            // 一级分类改变时
            this.data.cate_show_data[1] = cate_all_data[index].children;
            this.data.cate_show_data[2] = cate_all_data[index].children[0].children;
            sel_index_arr = [index, 0, 0];
        }
        if (e.detail.column == 1) {
            // 二级分类改变时
            this.data.cate_show_data[2] = cate_show_data[1][index].children;
            sel_index_arr[1] = index;
            sel_index_arr[2] = 0;
        }
        if (e.detail.column == 2) {
            // 三级分类改变时
            sel_index_arr[2] = index;
        }
        // 重新渲染
        this.setData({
            cate_show_data: this.data.cate_show_data,
            sel_index_arr: sel_index_arr
        });
    },

    // 选择类型时
    dataTypeChage: function (e) {
        this.setData({
            data_type_name: this.data.data_type_list[e.detail.value].name,
            data_type_value: this.data.data_type_list[e.detail.value].value,
            season_list: [],
            order_list: [],
            now_page: 0,
            keywords: '',
            list_total: 0,
        });
        // 重新请求数据
        this.get_season_list();
    },

    // 提示
    show_full_field: function (e) {
        wx.showToast({
            title: e.currentTarget.dataset.field,
            icon: 'none',
            duration: 1500,
        });
        // 返回
        return false;
    },

    // 删除年份
    DeleteYear: function (e) {
        var index = e.currentTarget.dataset.index;
        // 删除对应年份
        this.data.year.splice(index, 1);
        this.setData({
            year: this.data.year,
            season_list: [],
            order_list: [],
            now_page: 0,
            keywords: '',
            list_total: 0,
        })
        // 重新请求数据
        this.get_season_list();
    },

    // 删除分类
    DeleteCategory: function (e) {
        var index = e.currentTarget.dataset.index;
        this.data.choose_id_arr.splice(index, 1);
        this.data.choose_index_arr.splice(index, 1);
        this.data.choose_cate_name.splice(index, 1);
        this.setData({
            choose_id_arr: this.data.choose_id_arr,
            choose_index_arr: this.data.choose_index_arr,
            choose_cate_name: this.data.choose_cate_name,
            season_list: [],
            order_list: [],
            now_page: 0,
            keywords: '',
            list_total: 0,
        })
        // 重新请求数据
        this.get_season_list();
    },


})
